﻿
function FormatNumber(str)
{
	
	var strTemp = GetNumber(str);
		
	if(strTemp.length <= 3)
		return strTemp;

	strResult = "";
	for(var i =0; i< strTemp.length; i++)
		strTemp = strTemp.replace(",", "");
	
	for(var i = strTemp.length; i>=0; i--)
	{
		if(strResult.length >0 && (strTemp.length - i -1) % 3 == 0)
			strResult = "," + strResult;
					
		strResult = strTemp.substring(i, i + 1) + strResult;
	}	
	return strResult;
	
	
}

function IsNumber(str)
{
	for(var i = 0; i < str.length; i++)
	{	
		var temp = str.substring(i, i + 1);
		
		if(!(temp == "," || (temp >= 0 && temp <=9)))
			return false;
	}
	return true;
}

function GetNumber(str)
{
	for(var i = 0; i < str.length; i++)
	{	
		var temp = str.substring(i, i + 1);		
		if(!(temp == "," || (temp >= 0 && temp <=9)))
		{
			alert("Không phải là số!");
			return str.substring(0, i);
		}
		
	}
	return str;
}
// JScript File
function IsNumeric(sText)
    {
       var ValidChars = "0123456789.";
       var IsNumber=true;
       var temp = 0;
       if(sText.charAt(0) == ".")
          {
            return false;
          }
       for (i = 0; i < sText.length && IsNumber == true; i++) 
       { 
          if (ValidChars.indexOf(sText.charAt(i)) == -1)
          {
             IsNumber = false;
          }
          if(sText.charAt(i) == ".")
          {
            temp = temp + 1;
          }
          if(temp > 1)
          {
            IsNumber = false;
          }
       }
       return IsNumber;
   }
   
//Cong moi them
function ValidateNumberKeyPress(field, evt)
{
   var charCode = (evt.which) ? evt.which : event.keyCode
   var keychar = String.fromCharCode(charCode);

   if (charCode > 31 && ((charCode != 45 && charCode < 48) || charCode > 57) && keychar != ".")
   {
      return false;
   }
   if (keychar == "." && field.value.indexOf(".") != - 1)
   {
      return false;
   }
   if(keychar == "-")
   {
      if (field.value.indexOf("-") != - 1 /* || field.value[0] == "-" */ )
      {
         return false;
      }
      else
      {
         // save caret position
         var caretPos = getCaretPosition(field);
         if(caretPos != 0)
         {
            return false;
         }
      }
   }

   return true;
}

function getCaretPosition(objTextBox)
{

   var objTextBox = window.event.srcElement;

   var i = objTextBox.value.length;

   if (objTextBox.createTextRange)
   {
      objCaret = document.selection.createRange().duplicate();
      while (objCaret.parentElement() == objTextBox &&
      objCaret.move("character", 1) == 1) -- i;
   }
   return i;
}

function ValidateNumberKeyUp(field,min,max)
{
    if(document.selection.type == "Text")
    {
        return;
    }

     //save caret position
    var caretPos = getCaretPosition(field);
    
    var fdlen = field.value.length;

    UnFormatNumber(field);

    var IsFound = /^-?\d+\.{0,1}\d*$/.test(field.value);
    if(!IsFound)
    {
        setSelectionRange(field, caretPos, caretPos);
        return false;             
    }
    
    field.value = FormatNumbers(field.value,2,',','.','','','-','',min,max);
    
    fdlen = field.value.length - fdlen;

    
    setSelectionRange(field, caretPos+fdlen, caretPos+fdlen);
}

function ValidateAndFormatNumber(NumberTextBox,min,max)
{
    if(NumberTextBox.value == "") 
        NumberTextBox.value = '0';
    
    UnFormatNumber(NumberTextBox);

    var IsFound = /^-?\d+\.{0,1}\d*$/.test(NumberTextBox.value);
    if(!IsFound)
    {
//      alert("Not a number");
        NumberTextBox.focus();
        NumberTextBox.select();  
        return;             
    }
    
    if(isNaN(parseFloat(NumberTextBox.value)))
    {
//      alert("Number exceeding float range");
        NumberTextBox.focus();
        NumberTextBox.select();               
    }
    //formatNumber(mynum,2,' ','.','','','-','') 
    NumberTextBox.value = FormatNumbers(NumberTextBox.value,2,',','.','','','-','',min,max);
}

function UnFormatNumber(obj)
{
    if(obj.value == "") return;
    
    obj.value = obj.value.replace(/,/gi, "");
}

function setSelectionRange(input, selectionStart, selectionEnd)
{
   if (input.setSelectionRange)
   {
      input.focus();
      input.setSelectionRange(selectionStart, selectionEnd);
   }
   else if (input.createTextRange)
   {
      var range = input.createTextRange();
      range.collapse(true);
      range.moveEnd('character', selectionEnd);
      range.moveStart('character', selectionStart);
      range.select();
   }
}

// number formatting function
// copyright Stephen Chapman 24th March 2006, 22nd August 2008
// permission to use this function is granted provided
// that this copyright notice is retained intact

function FormatNumbers(num, dec, thou, pnt, curr1, curr2, n1, n2,min,max)
{
   if(num < min)
   num = min;
   if(num > max)
   num = max;
   var x = Math.round(num * Math.pow(10, dec));
   if (x >= 0)
   n1 = n2 = '';
   var y = ('' + Math.abs(x)).split('');
   var z = y.length - dec;
   if (z < 0) z -- ;
   for(var i = z; i < 0; i ++ ) y.unshift('0');
   if (z < 0) z = 1;
   y.splice(z, 0, pnt);
   if(y[0] == pnt) y.unshift('0');
   while (z > 3)
   {
      z -= 3;
      y.splice(z, 0, thou);
   }
var r = curr1 + n1 + y.join('') + n2 + curr2;      
// bo n1 de bo dau -
//   var r = curr1 + y.join('') + n2 + curr2;      
   return r;
}
